import Vue from 'vue'
import Vuex from 'vuex'
import {
	clearState
} from '@/utils/index.js'
import { log } from 'util'

Vue.use(Vuex)

const store = new Vuex.Store({
	state: {
		//token
		token: uni.getStorageSync("token"),
		//token到期时间戳
		expiretime: uni.getStorageSync("expiretime"),
		//售后表单
		afterSale: {
			customer_type: 1,
			address_id: 0,
			user_address_id: 0,
			express_number: '',
			express_number_img: '', //快递单号图片地址
			as_type: 1,
			user_province: '',
			user_city: '',
			user_county: '',
			user_area: '',
			pickup_start_time: '',
			product: [{
				product_model_id: {}, //产品型号编号
				fault_name_list: [], //故障类型编号
				fault_name: [],
				serial_number: '', //序列号
				product_name: [], //寄回清单名称
				fault_desc: '', //故障描述
				fault_file: '', //故障图片
				fileList: [], //故障图片回显
			}]
		},
		faultList: [],
		//显示寄回和寄出地址
		address: {
			//寄出地址
			person: {
				name: '',
				phone: '',
				addr: ''
			},
			//寄回地址
			firm: {
				name: '',
				phone: '',
				addr: ''
			}
		},
		editAddress: {
			//寄出地址
			person: {},
			//寄回地址
			firm: {}
		},
		//微信头像信息
		wxInfo: {
			avatar: uni.getStorageSync("wxAvatar"),
			nickName: uni.getStorageSync("wxNickName")
		},
		//手机号码
		mobile: uni.getStorageSync("mobile"),
		//服务热线
		hotline: '',
		//服务政策图
		policyImg: '',
		//openId 和 sessino_key
		openid: uni.getStorageSync("openid"),
		session_key: uni.getStorageSync("session_key"),
	},
	mutations: {
		editToken(state, val) {
			state.token = val
		},
		//修改微信信息
		editWxinfo(state, item) {
			state.wxInfo.avatar = item.avatarUrl
			state.wxInfo.nickName = item.nickName
		},
		//修改手机号码
		editMobile(state, val) {
			state.mobile = val
		},
		//增加产品型号
		addProduct(state, val) {
			state.afterSale.product.push({
				product_model_id: {},
				fault_name_list: [],
				fault_name: '',
				serial_number: '',
				product_name: '',
				fault_desc: '',
				fault_file: '',
				fileList: []
			})
		},
		//修改故障图片
		editFaultImg(state, val) {
			state.afterSale.product[val.index].fault_file = val.url
			state.afterSale.product[val.index].fileList[0] = {
				url: val.fullurl,
				status: 'success'
			}
		},
		//删除图片
		deletePic(state, val) {
			state.afterSale.product[val.index].fileList.splice(val.imgIndex, 1)
		},
		//修改快递单号图片
		editNumberImg(state, val) {
			state.afterSale.express_number_img = val
		},
		//删除数据结构
		deleteBox(state, index) {
			state.afterSale.product.splice(index, 1)
		},
		//修改客户类型
		editType(state, val) {
			state.afterSale.customer_type = val
		},
		//修改寄回地址编号
		editAddressId(state, obj) {
			state.afterSale.address_id = obj.id
			state.address.firm = obj
			console.log(obj,'修改了寄回地址');
		},
		//修改寄出地址编号
		editUserAddressId(state, obj) {
			console.log('-----修改寄出地址编号', obj)
			state.afterSale.user_address_id = obj.id
			state.address.person = obj
		},
		//寄修记录详情的地址
		editAddress(state, obj, userObj) {
			state.editAddress.person = obj
			console.log(obj,'修改寄出地址');
		},
		//修改寄回地址
		editFirmAddress(state, obj, userObj) {
			state.editAddress.firm = obj
		},
		//修改故障类型编号
		editFaultId(state, val) {
			if (!state.afterSale.product[val.index].fault_name_list.includes(val.item.name)) {
				//state.afterSale.product[val.index].fault_name_list.push(val.item.name)
				state.afterSale.product[val.index].fault_name_list = val.item.name
				state.afterSale.product[val.index].fault_name = state.afterSale.product[val.index]
					.fault_name_list.toString()
			}
		},
		//修改产品型号编号
		editProductId(state, val) {
			state.afterSale.product[val.index].product_model_id = val.item
		},
		//修改取件时间 
		editTime(state, val) {
			state.afterSale.pickup_start_time = val
		},
		//修改类型
		editAsType(state, val) {
			state.afterSale.as_type = val
		},
		//修改服务热线和服务政策图
		editPolicy(state, val) {
			state.policyImg = val.policy
			state.hotline = val.hotline
		},
		//重置afterSale数据
		clearAfterSale(state) {
			state.afterSale = {
				customer_type: 1,
				address_id: uni.getStorageSync('user_address_id'),
				user_address_id: uni.getStorageSync('sendId'),
				express_number: '',
				express_number_img: '', //快递单号图片地址
				as_type: uni.getStorageSync('as_type'),
				user_province: '',
				user_city: '',
				user_county: '',
				user_area: '',
				pickup_start_time: '',
				product: []
			}
		},
		//修改openId和session_key
		editOpenId(state, val) {
			state.openid = val.openid
			state.session_key = val.session_key
		},
		//修改寄回清单
		editProduct_name(state, obj) {
			state.afterSale.product[obj.index].product_name = obj.val
		},
		//修改故障类型
		editFault_name(state, obj) {
			state.afterSale.product[obj.index].fault_name = obj.val
		},
		//修改故障图片和描述
		editFault_desc(state, obj) {
			state.afterSale.product[obj.index].fault_desc = obj.val
		}
	}
})

export default store
